﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using SyncList.Scanner;
using SyncList.ValueObject;
using System.Diagnostics;
using SyncList.Persistance;
using System.IO;
using System.Security.Permissions;
using Microsoft.Win32;

namespace SyncList
{
    /// <summary>
    /// Logique d'interaction pour Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void tbDirectoryToScan_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog().Equals(System.Windows.Forms.DialogResult.OK))
            {
                tbDirectoryToScan.Text = fbd.SelectedPath;
            }

            if (!string.IsNullOrWhiteSpace(tbDirectoryToScan.Text))
            {
                long minSize = 1 * 1024;

                string fileName = "SyncList.xml";

                while (true)
                {

                    Import import = new Import();
                    Data data = import.ImportXml(fileName);

                    FileScanner scanner = new FileScanner();

                    int count = scanner.Scan(data.Files, tbDirectoryToScan.Text, 0, minSize, new TimeSpan(0, 0, 15));
                    if (count == 0) break;

                    Export export = new Export();
                    export.ExportXml(data, fileName, minSize);

                    TextBlockCaption.Text = "Total : " + data.Files.Count.ToString() + " files read";
                }
                System.Windows.MessageBox.Show("FIN");
            }
        }

        private void tbFileExtension_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            if (ofd.ShowDialog().Equals(System.Windows.Forms.DialogResult.OK))
            {
                string _selectedFile = ofd.FileName;

                //on lit le contenu du fichier sélectionné dans un stream de bytes
                if (File.Exists(_selectedFile))
                {
                    using (Stream fileStream = new FileStream(_selectedFile, FileMode.Open, FileAccess.Read))
                    {
                        byte[] buffer = new byte[20];
                        fileStream.Read(buffer,0,20);
                        string stringAppend = string.Empty;
                        foreach (byte b in buffer)
                        {
                            stringAppend += b.ToString();
                        }
                        TextBlockCaption.Text += ("\n" + new FileInfo(_selectedFile).Extension + "\t" + GetMIMEType(_selectedFile) + "\t" + "Extension");
                    }
                }
            }
        }


        public static string GetMIMEType(string filepath)
        {
            RegistryPermission regPerm = new RegistryPermission(RegistryPermissionAccess.Read, "\\HKEY_CLASSES_ROOT");
            RegistryKey classesRoot = Registry.ClassesRoot;
            FileInfo fi = new FileInfo(filepath);
            String dotExt = fi.Extension.ToLower();
            RegistryKey typeKey = classesRoot.OpenSubKey(@"MIME\Database\Content Type");
            String Keyname = "";
            foreach (string keyname in typeKey.GetSubKeyNames())
            {
                RegistryKey curKey = classesRoot.OpenSubKey(@"MIME\Database\Content Type\" + keyname);
                if ((String)curKey.GetValue("Extension") == dotExt)
                {
                    Keyname = keyname;
                }
            }
            return Keyname;
        }
    }
}
